using System;
using System.Collections;
using System.Collections.Generic;
using Datas;
using UnityEngine;
using UnityEngine.EventSystems;
public class BackManage : MonoBehaviour, IPointerClickHandler
{
  
   
    /**背包的格子*/
    public GameObject slotGrid;
    /**槽*/
    public BackSlot SlotPre;
    
    /**草药数据集合*/
    public HerbalList mCarList;
    /**背包的数据*/
    private List<int> bag;
    private void OnEnable()
    {
        RefreshNum();
    }

    void CreateNewbackSlot(int carId,int bagID)
    {
        BackSlot newSlot = Instantiate(SlotPre, slotGrid.transform);

        newSlot.slotItem = mCarList.MedicincCarLists[carId];
        newSlot.bagId = bagID;
        newSlot.slotText.text = mCarList.MedicincCarLists[carId].HerbalTitle;
        newSlot.slotImage.sprite = mCarList.MedicincCarLists[carId].HerbalImage;
    }

     void RefreshNum()
     {
         bag=PlayerData.GetCardIds();
        Debug.Log(bag.Count);
        for (int i = 0; i <slotGrid.transform.childCount; i++)
        {
            if (slotGrid.transform.childCount == 0)
            {
                break; 
            }
            Destroy(slotGrid.transform.GetChild(i).gameObject);
        }
        
        for (int i = 0; i <bag.Count ; i++)
        {
            CreateNewbackSlot(bag[i],i);
        }
        
    }

    public void DeleteBack()
     {
         Destroy(transform.gameObject);
     }
    
    public void OnPointerClick(PointerEventData eventData)
    {
        // 关闭界面
        Destroy(gameObject);
    }
}
